package com.loooooo.pm2024.utils;

import com.loooooo.pm2024.exception.MyBaseException;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;

public class IOUtils {

    public static String readString(File file) {
        if (!file.exists()) return null;
        try {
            byte[] buff = Files.readAllBytes(Paths.get(file.getAbsolutePath()));
            return new String(buff, "UTF-8");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
//        RandomAccessFile f = null;
//        try {
//            f = new RandomAccessFile(file.getAbsolutePath(), "r");
//            StringBuilder stringBuilder = new StringBuilder();
//            String str;
//            while ((str = f.readLine()) != null) {
//                stringBuilder.append(new String(str.getBytes(StandardCharsets.UTF_8), "UTF-8"));
//            }
//            return stringBuilder.toString();
//        } catch (Exception e) {
//            e.printStackTrace();
//        } finally {
//            try {
//                f.close();
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//        }
//        return null;
    }

    public static String copy(File file, File to) {
        if (!file.exists()) return null;
        if (to.exists()) to.delete();
        System.out.println(String.format("copy2 %s %s", file.getAbsolutePath(), to.getAbsolutePath()));
        try {
            byte[] buff = Files.readAllBytes(Paths.get(file.getAbsolutePath()));
            write(to, buff);
        } catch (IOException e) {
            e.printStackTrace();
            MyBaseException.throwMe(10500, e.getMessage());
        }
        return null;
    }

    public static void writeString(File file, String text) {
        OutputStreamWriter osw = null;
        if (!file.getParentFile().exists()) file.getParentFile().mkdirs();
        try {
            FileOutputStream outputStream = new FileOutputStream(file);
            osw = new OutputStreamWriter(outputStream, StandardCharsets.UTF_8.name());
            osw.write(text);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                osw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        //write(file, text.getBytes(StandardCharsets.UTF_8));
    }

    public static void write(File file, byte[] data) {
        FileOutputStream outputStream = null;
        if (!file.getParentFile().exists()) file.getParentFile().mkdirs();
        System.out.println(String.format("copy3 %s ", file.getAbsolutePath()));
        try {
            outputStream = new FileOutputStream(file);
            outputStream.write(data);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                outputStream.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static void copyInputStreamToFile(InputStream inputStream, File file) throws IOException {

        if (!file.getParentFile().exists()) file.getParentFile().mkdirs();
        try (FileOutputStream outputStream = new FileOutputStream(file)) {

            int read;
            byte[] bytes = new byte[1024];

            while ((read = inputStream.read(bytes)) != -1) {
                outputStream.write(bytes, 0, read);
            }
        }
    }

    public static void write(InputStream inputStream, OutputStream outputStream) {

        int read;
        byte[] bytes = new byte[1024];

        try {
            while ((read = inputStream.read(bytes)) != -1) {
                outputStream.write(bytes, 0, read);
            }
        } catch (Exception ex) {

        }
    }

    public static void write(File file, OutputStream outputStream) {

        try {
            FileInputStream inputStream = new FileInputStream(file);
            write(inputStream, outputStream);
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
}
